/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.util.ArrayList;
import model.entidades.Pedido;
import model.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Administrador
 */
public class PedidoDAOImpl implements PedidoDAO{
Transaction transaccion;
    Session s;
    public boolean agregar(Pedido pedido) {
        
        try {
            s=HibernateUtil.getSessionFactory().getCurrentSession();
            transaccion = s.beginTransaction();
            s.save(pedido);
            transaccion.commit();

            return true;
        } catch (Exception e) {
            if (transaccion != null) 
                transaccion.rollback();
                return false;
            }
    }

    public boolean borrar(Pedido pedido) {
       try
       { 
           s = HibernateUtil.getSessionFactory().getCurrentSession();
           transaccion=s.beginTransaction();
           s.delete(pedido);//borra info
           transaccion.commit();//cambios a la bd
           return true;
       }
       catch(Exception e)
       {
           if(transaccion!=null)
               transaccion.rollback();
           
           return false;
       }
    }

    public ArrayList<Pedido> listar() {
        try {
             s = HibernateUtil.getSessionFactory().getCurrentSession();
             transaccion=s.beginTransaction();
            return (ArrayList<Pedido>)s.createQuery("From Pedido").list();
        } catch (Exception e) {
        return null;
        }
    }
    
}
